package wicket;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormChoiceComponentUpdatingBehavior;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.Radio;
import org.apache.wicket.markup.html.form.RadioGroup;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import plantasVsZombies.AdministradorJardinZen;
import plantasVsZombies.AlmanaqueZombie;
import plantaszombies.Partida;
import plantaszombies.Semilla;
import plantaszombies.Zombie;

public class JardinZenPage extends WebPage {

	private static final long serialVersionUID = 1L;

	private AdministradorJardinZen administrador;
	private WebPage paginaAnterior;

	private Button mejorarButton;

	public JardinZenPage(TableroPage tableroPage, Partida partida) {
		this.paginaAnterior = tableroPage;
		this.administrador = new AdministradorJardinZen(partida);
		this.addPlantasForm();

	}

	protected void addPlantasForm() {
		Form<AdministradorJardinZen> plantasform = new Form<AdministradorJardinZen>(
				"plantasForm", new CompoundPropertyModel(this.administrador));

		plantasform.add(new Label("recursos"));
		this.generarCamposBusqueda(plantasform);
		this.generarGrillaResultados(plantasform);
	//	plantasform.add(new FeedbackPanel("feedbackPanel"));
		this.addButtons(plantasform);
		this.add(plantasform);
	}

	private void generarCamposBusqueda(Form<AdministradorJardinZen> plantasform) {
		plantasform.add(new TextField<String>("nombreAbuscar"));
	}

	private void generarGrillaResultados(Form<AdministradorJardinZen> plantasform) {
		final RadioGroup<Semilla> radioGroup = new RadioGroup<Semilla>("semillaSeleccionada");
		
		radioGroup.add(new PropertyListView<Semilla>("semillasSelect") {
			@Override
			protected void populateItem(final ListItem<Semilla> item) {
				item.add(new Radio<Semilla>("radioSemilla",	item.getModel()));
				item.add(new Label("nombre"));
				item.add(new Label("puntosDeDanio"));
				item.add(new Label("capacidadDefensiva"));
			}
		});
		radioGroup.add(new AjaxFormChoiceComponentUpdatingBehavior() {
			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				target.add(mejorarButton);
			}

		});
		
		plantasform.add(radioGroup);
	}

	
	private void addButtons(Form<AdministradorJardinZen> plantasform) {
		plantasform.add(new Button("buscar") {
			@Override
			public void onSubmit() {
				JardinZenPage.this.administrador.buscarWicket();
			}

		});
		plantasform.add(new Button("volver") {
			@Override
			public void onSubmit() {
				this.setResponsePage(JardinZenPage.this.getPaginaAnterior());
			}
		});
		plantasform.add(new Button("irAlOtroJardin") {
			@Override
			public void onSubmit() {
				JardinZenPage.this.administrador.irAlOtroJardin();
			}
		});

		plantasform.add(new Button("limpiar") {
			@Override
			public void onSubmit() {
				JardinZenPage.this.administrador.limpiarBusqueda();
			}
		});

		plantasform.add(this.mejorarButton = new Button("mejorar") {
			@Override
			public void onSubmit() {
				this.setResponsePage(new MejorasPage(JardinZenPage.this,
						JardinZenPage.this.administrador));
			}
		});
		
		plantasform.add(new Link<Object>("linkNombre") {
			@Override
			public void onClick() {
				JardinZenPage.this.administrador.buscarWicketOrdenadorPor("nombreParaOrdenar");
			}
		});
		plantasform.add(new Link<Object>("linkAtaque") {
			@Override
			public void onClick() {
				JardinZenPage.this.administrador.buscarWicketOrdenadorPor("danio");
			}
		});
		plantasform.add(new Link<Object>("linkCapDef") {
			@Override
			public void onClick() {
				JardinZenPage.this.administrador.buscarWicketOrdenadorPor("capDefensiva");
			}
		});

	}

	// Accesors
	public WebPage getPaginaAnterior() {
		return paginaAnterior;

	}

	public void setPaginaAnterior(WebPage paginaAnterior) {
		this.paginaAnterior = paginaAnterior;
	}

	public AdministradorJardinZen getAdministrador() {
		return administrador;
	}

	public void setAdministrador(AdministradorJardinZen administrador) {
		this.administrador = administrador;
	}
}
